package com.codeoss.module.sale.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.module.sale.dao.SaleOrderLineMapper;
import com.codeoss.module.sale.model.SaleOrderLine;
import com.codeoss.module.sale.service.ISaleOrderLineService;
import com.codeoss.module.uom.service.IUomUomConversionService;

@Service
public class SaleOrderLineService extends BaseService<SaleOrderLineMapper, SaleOrderLine>
		implements ISaleOrderLineService {
	@Autowired
    private IUomUomConversionService uomUomConversionService;
	@Transactional
	public void insert(SaleOrderLine saleOrderLine) {
		beforeSave(saleOrderLine,true);
		super.insert(saleOrderLine);
	}
	@Transactional
	public void update(SaleOrderLine saleOrderLine) {
		beforeSave(saleOrderLine,false);
		super.update(saleOrderLine);
	}
	@Transactional
	protected void beforeSave(SaleOrderLine saleOrderLine,boolean isNewRecord) {
		Long productId=saleOrderLine.getProductProductId();
	    BigDecimal qtyEntered=saleOrderLine.getQtyEntered();
	    Long uomUomId=saleOrderLine.getUomUomId();
	    if(productId!=null&&qtyEntered!=null&&uomUomId!=null) {
	    	BigDecimal qtyOrdered=uomUomConversionService.convert(productId, uomUomId, qtyEntered);
	    	saleOrderLine.setQtyOrdered(qtyOrdered);
	    }
	}

	@Override
	public List<SaleOrderLine> getLineListBySaleOrderId(Long saleOrderId) {
		// TODO Auto-generated method stub
		QueryWrapper<SaleOrderLine> query=new QueryWrapper<SaleOrderLine>();
		query.eq("sale_order_id", saleOrderId);
		return list(query);
	}
}